package com.fiewolf.lol.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fiewolf.lol.dao.IMenuDao;
import com.fiewolf.lol.entities.Menu;

@Service
public class MenuService implements IMenuService{

	@Autowired
	private IMenuDao menuDao;
	
	@Override
	public Collection<Menu> findAllMenus(String userId) {
		List<Menu> allMenus = menuDao.findAllMenus(userId);
		Map<Integer, Menu> map = new HashMap<>();
		for(Menu m : allMenus){
			map.put(m.getId(), m);
		}
		for(int i =allMenus.size()-1;i>0;i--){
			Menu m = allMenus.get(i);
			if(map.containsKey(m.getParentid())){
				map.get(m.getParentid()).getMenus().add(m);
				map.remove(m.getId());
			}
		}
		return map.values();
	}
	
}
